package com.bub.pay.api.controller;

import com.bub.pay.core.entity.vo.user.GoogleVo;
import com.bub.pay.core.entity.vo.user.PasswordVo;
import com.bub.pay.core.manager.merchant.MerUserManager;
import com.bub.pay.core.manager.role.RoleManager;
import com.bub.pay.core.manager.user.UserCommonManager;
import com.bub.pay.core.manager.user.UserManager;
import com.bub.pay.framework.common.entity.model.SessionModel;
import com.bub.pay.framework.common.util.ContextHolder;
import com.bub.pay.framework.redis.RedisClient;
import com.bub.pay.framework.redis.RedisKeyConstant;
import com.bub.pay.framework.redis.RedisKeyPrefix;
import com.bub.pay.framework.wrap.annotation.SysLog;
import com.bub.pay.framework.wrap.annotation.SysLogType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * @author wing
 */
@RestController
@SysLogType("用户")
@RequestMapping("/merchant/user")
public class UserController extends BaseController {
    protected static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private MerUserManager meruserManager;

    @Resource
    private UserManager userManager;

    @Resource
    private RoleManager roleManager;

    @Resource
    private UserCommonManager userCommonManager;

    @Resource
    private RedisClient redisClient;

    @SysLog("是否需要再次输入支付密码")
    @GetMapping("/checkPayPassNeedInput")
    public Object checkPayPassNeedInput() {
        SessionModel sessionModel = ContextHolder.currentUserInfo();
        String key = String.format("%s:%s%s", RedisKeyPrefix.USER, RedisKeyConstant.MER_PAY_PWD, sessionModel.getUserId());
        return redisClient.get(key) == null;
    }

    @SysLog("商户绑定谷歌验证码")
    @PostMapping("/bindGoogle")
    public Object bindGoogle(HttpServletRequest httpRequest, @RequestBody @Valid GoogleVo reqVo) throws Exception {
        SessionModel sessionModel = ContextHolder.currentUserInfo();
        return userCommonManager.bindGoogle(sessionModel.getUserId().toString(), reqVo);
    }

    @PostMapping("/queryBindGoogle")
    public Object queryBindGoogle() throws Exception {
        SessionModel sessionModel = ContextHolder.currentUserInfo();
        return userCommonManager.queryBindGoogle(sessionModel.getUserId().toString());
    }

    @PostMapping("/refreshBindGoogle")
    public Object refreshBindGoogle() throws Exception {
        SessionModel sessionModel = ContextHolder.currentUserInfo();
        return userCommonManager.refreshBindGoogle(sessionModel.getUserId().toString());
    }

    @SysLog("删除谷歌验证码")
    @PostMapping("/removeGoogle")
    public Object removeGoogle(@RequestBody @Valid GoogleVo reqVo) throws Exception {
        SessionModel sessionModel = ContextHolder.currentUserInfo();
        return userCommonManager.removeGoogle(sessionModel.getUserId().toString(), reqVo);
    }

    @SysLog("商户修改登陆密码")
    @PostMapping("/changePassword")
    public Object changePassword(@RequestBody @Valid PasswordVo reqVo)
            throws Exception {
        SessionModel sessionModel = ContextHolder.currentUserInfo();
        return userCommonManager.changePassword(sessionModel.getUserId().toString(), reqVo);
    }

    @SysLog("商户修改支付密码")
    @PostMapping("/changePayPassword")
    public Object changePayPassword(@RequestBody @Valid PasswordVo reqVo)
            throws Exception {
        SessionModel sessionModel = ContextHolder.currentUserInfo();
        return userCommonManager.changePayPassword(sessionModel.getUserId().toString(), reqVo);
    }


}
